<template>
  <div class="app-container">
    <el-form :inline="true" ref="refForm" :model="form">
      <el-form-item label="查询时间"  prop="time">
        <el-date-picker
          v-model="form.time"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item v-if="roleCheck('virtual')" class="with-pre-select" label="数据类型">
        <el-select v-model="form.isFake">
          <el-option v-for="item in consts.STATISTICS_RANGE"
                     :key="item.value"
                     :label="item.title"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-button class="filter-item" type="primary"  icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
      <el-button v-if="roleCheck('export')" class="filter-item" type="primary" icon="el-icon-download" @click="exportExcel">导出</el-button>
    </el-form>
    <el-table
      border
      :data="tableData"
      v-loading="loading"
      element-loading-text="加载中..."
      show-summary
      :summary-method="getLocalSummaries"
      class="el-table-align-center"
      :default-sort = "{prop: 'day', order: 'descending'}">
    <el-table-column label="时间" sortable prop="day" width="180" align="center">
      </el-table-column>
      <el-table-column prop="active" label="活跃人数" align="center"></el-table-column>
      <el-table-column prop="maxOnline" label="最高在线" align="center"></el-table-column>
      <el-table-column prop="register" label="注册人数" align="center"></el-table-column>
      <el-table-column prop="recharge" label="充值人数" align="center"></el-table-column>
      <el-table-column prop="betting" label="投注人数" align="center"></el-table-column>
      <el-table-column prop="withdraw" label="提现人数" align="center"></el-table-column>
    </el-table>
  </div>
</template>


<script>
  import {getStatisticalMembers } from "@/services/api/statistical";
  import {grid,handleDownload} from '@/build'
  export default {
    name: "statistics-members",
    mixins: [grid,handleDownload],
    data() {
      // let times = _.toPeriod({
      //   start: [-1, 'days'],
      //   end: [0, 'days'],
      //   format: 'YYYY-MM-DD'
      // });
      // 本月时间本月1号至当前时间
      const monthTime = [moment().startOf('month').format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')]


      return {
        localSummariesText: '汇总',
        tableData: [],
        form:{
          time:monthTime,
          isFake: 0,
        },
        filters:{
          stime:moment(monthTime[0]).valueOf(),
          etime:moment(monthTime[1]).valueOf(),
          export:undefined,
          isFake: 0,
        },
      }
    },
    methods: {
      submitFilterForm(){
        const {time,isFake} = this.form;
        let stime = undefined, etime = undefined;
        if (!_.isUndefined(time) && _.isArray(time)) {
          stime = moment(time[0]).valueOf();
          etime = moment(time[1]).valueOf()+ 86399999;
        }
        this.filters = {...this.filters, stime, etime, isFake, project: consts.PLATFROM_PREFIX}
        this.gridSearch()
      },

    },
    mounted() {
      this.bindGet(getStatisticalMembers, 'tableData', {
        action: 'getGrid',
        sendingData: 'filters',
        format: data => {
          let result = [];
          _.forIn(data, (value, key) => {
            result.push({...value, day: key})
          })
          result = _.sortBy(result,(item)=>{
            return -item.day;
          });
          return result;
        }
      });
      this.submitFilterForm()
    }

  }
</script>

<style scoped>

</style>
